package cn.zmwh.portal.modules.index.controller;


import cn.hutool.core.util.RandomUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.portal.modules.index.pojo.entity.TlCircle;
import cn.zmwh.portal.modules.index.pojo.vo.TlCircleVo;
import cn.zmwh.portal.modules.index.service.TlCircleService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dmzmwh
 * @since 2024-09-22
 */
@Slf4j
@RestController
@RequestMapping("/index/circle")
public class TlCircleController {


    @Autowired
    TlCircleService tlCircleService;

    @PostMapping
    @ApiOperation(value = "圈子")
    public Result save(@RequestBody TlCircle req) {
        if (req.getId() == null) {
            req.setCreateTime(new Date());
            tlCircleService.save(req);
        } else {
            req.setCreateTime(null);
            tlCircleService.updateById(req);
        }
        return Result.success();
    }

    @GetMapping
    @ApiOperation(value = "话题")
    public Result list() {
        List<TlCircle> list = tlCircleService.list(Wrappers.<TlCircle>lambdaQuery()
                .eq(TlCircle::getHandpick, 1)
                .orderByDesc(TlCircle::getSort));
        List<TlCircleVo> objects = new ArrayList<>();
        for (TlCircle tlCircle : list) {
            TlCircleVo tlCircleVo = new TlCircleVo();
            BeanUtils.copyProperties(tlCircle, tlCircleVo);
            tlCircleVo.setFollowNum(RandomUtil.randomInt(10, 4000));
            objects.add(tlCircleVo);
        }

        return Result.ok(objects);
    }

}

